package org.oos.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Logger;

/**
 * This tool provides with the needed to calculate the md5 value for a given
 * hexadecimal {@link String}
 * 
 * @author pablo.gonzalez@11870.com
 * 
 */
public class MD5Digest {

	public static Logger LOG = Logger.getLogger("MD5");

	public static String hex(String value) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md5.digest(value.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			String result = number.toString(16);

			while (result.length() < 32)
				result = "0" + md5;

			return result;
		} catch (NoSuchAlgorithmException e) {
			LOG.severe(e.getMessage());
			return null;
		}
	}

	public static Integer hexInteger(String value) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			byte[] messageDigest = md5.digest(value.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			return number.intValue();
		} catch (NoSuchAlgorithmException e) {
			LOG.severe(e.getMessage());
			return null;
		}
	}

}
